package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.Ask;
import com.wxxymaker.edu.roles.CourseLearnRoles;
import com.wxxymaker.edu.roles.StudentRoles;
import com.wxxymaker.edu.roles.TeacherRoles;
import com.wxxymaker.edu.service.AskService;
import com.wxxymaker.edu.service.CourseService;
import com.wxxymaker.edu.utils.TimeUtils;
import com.wxxymaker.edu.utils.TokenUtils;
import io.github.biezhi.anima.page.Page;

/**
 * @author zyx
 * @date 2018/9/27 - 21:31
 * 用户提问相关操作接口
 */
@CourseLearnRoles
@Path(value = "/course/ask/api", restful = true)
public class AskHandlerApi {

    @Inject
    private AskService askService;

    @Inject
    private CourseService courseService;

    /**
     * 添加问题
     * @return
     */
    @StudentRoles
    @PostRoute("/:kid/join/info/add")
    public RestResponse add(@PathParam int kid,
                            @Param Ask ask) {
        ask.setUserId(TokenUtils.currentUser().getId());
        ask.setTime(TimeUtils.getTime());
        ask.setCourseId(kid);
        Ask ask1 = askService.add(ask);
        Page<Ask> askPage = askService.getAskById(ask1.getId());
        if (askPage.getTotalRows() > 0) {
            return RestResponse.ok(askPage.getRows().get(0));
        }
        return RestResponse.fail();
    }

    @StudentRoles
    @PostRoute("/:kid/join/info/get")
    public RestResponse get(@PathParam int kid,
                            @Param(defaultValue = "1") int page,
                            @Param(defaultValue = "5") int size) {
        Page<Ask> askPage = askService.getByKid(kid, page, size);
        if (askPage.getTotalRows() > 0) {
            return RestResponse.ok(askPage);
        }
        return RestResponse.fail();
    }

    @StudentRoles
    @PostRoute("/:kid/join/getById")
    public RestResponse getByWid(@PathParam int kid,
                                 @Param Integer wid) {
        setLooks(wid);
        Page<Ask> askPage = askService.getAskById(wid);
        if (askPage.getTotalRows() > 0) {
            return RestResponse.ok(askPage.getRows().get(0));
        }
        return RestResponse.fail();
    }

    @StudentRoles
    @PostRoute("/:kid/join/getAskByKidWid")
    public RestResponse getAskByKidWid(@PathParam   int kid,
                                       @Param       int tid,
                                       @Param(defaultValue = "1") int page,
                                       @Param(defaultValue = "8") int size) {
        Page<Ask> askPage = askService.getAskByKidWid(kid, tid, page, size);
        if (askPage.getTotalRows() > 0) {
            return RestResponse.ok(askPage);
        }
        return RestResponse.fail();
    }

    @StudentRoles
    @PostRoute("/user/getAsk")
    public RestResponse getAskById(@Param(defaultValue = "1") int page,
                                   @Param(defaultValue = "8") int size) {
        Integer uid = TokenUtils.currentUser().getId();
        Page<Ask> askPage = askService.getAllAskById(uid, page, size);
        if (askPage.getTotalRows() > 0) {
            return RestResponse.ok(askPage);
        }
        return RestResponse.fail();
    }

    @TeacherRoles
    @PostRoute("/tea/getAsk")
    public RestResponse getAsk(@Param(defaultValue = "1") int page,
                               @Param(defaultValue = "8") int size) {
        Integer tid = TokenUtils.currentUser().getId();
        Page<Ask> askPage = askService.getAskByteaId(tid, page, size);
        if (askPage.getTotalRows() > 0) {
            return  RestResponse.ok(askPage);
        }
        return RestResponse.fail();
    }

    private Integer setLooks(Integer wid) {
        Page<Ask> askPage = askService.getAskById(wid);
        Ask ask = askPage.getRows().get(0);
        Integer i = askService.updateByWidlooks(ask.getId(), ask.getLooks());
        if(i > 0) {
            return 1;
        }
        return 0;
    }

}

